package com.yuan.algorithms.分治算法;

import java.util.Random;

/**
 * @author YouYuan
 * @date 2022/3/25 6:55
 */
public class SortUtil {

    /**
     * 验证数组是否有序
     * @param arr
     */
    public static void validateSort(int[] arr) {
        int max = Integer.MIN_VALUE;
        boolean isPass = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < max) {
                System.out.printf("排序数据异常！index:%d, value:%d, curMax:%d%n", i, arr[i], max);
                isPass = false;
                break;
            } else {
                max = arr[i];
            }
        }
        if (isPass) {
            System.out.printf("排序数据验证通过，数据长度：%d%n", arr.length);
        }
    }

    /**
     * 随机生成数组
     * @param length
     * @return
     */
    public static int[] randomArr(int length) {
        int[] arr = new int[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            arr[i] = random.nextInt();
        }
        return arr;
    }

    public static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    /**
     * 冒泡排序，时间复杂度O(n^2)
     * @param arr
     */
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                }
            }
        }
    }
}
